// 后端API路由，处理计算请求
export async function POST(request: Request) {
  try {
    const { num1, num2, operator } = await request.json();
    
    // 验证输入
    if (typeof num1 !== 'number' || typeof num2 !== 'number' || !operator) {
      return new Response(
        JSON.stringify({ error: '无效的输入参数' }),
        { status: 400, headers: { 'Content-Type': 'application/json' } }
      );
    }
    
    let result;
    
    // 根据运算符执行相应计算
    switch (operator) {
      case '+':
        result = num1 + num2;
        break;
      case '-':
        result = num1 - num2;
        break;
      case '*':
        result = num1 * num2;
        break;
      case '/':
        if (num2 === 0) {
          return new Response(
            JSON.stringify({ error: '除数不能为零' }),
            { status: 400, headers: { 'Content-Type': 'application/json' } }
          );
        }
        result = num1 / num2;
        break;
      case '%':
        result = num1 % num2;
        break;
      default:
        return new Response(
          JSON.stringify({ error: '不支持的运算符' }),
          { status: 400, headers: { 'Content-Type': 'application/json' } }
        );
    }
    
    // 返回计算结果
    return new Response(
      JSON.stringify({ result }),
      { status: 200, headers: { 'Content-Type': 'application/json' } }
    );
  } catch (error) {
    console.error('计算错误:', error);
    return new Response(
      JSON.stringify({ error: '服务器计算错误' }),
      { status: 500, headers: { 'Content-Type': 'application/json' } }
    );
  }
}
    